package com.bobo.web.provider.service.impl.order;

import com.bobo.web.api.constants.RedisConstant;
import com.bobo.web.api.domain.order.InsuranceOrder;
import com.bobo.web.api.vo.base.ResponseResult;
import com.bobo.web.api.vo.order.CreateSecKillParam;
import com.bobo.web.provider.dao.order.InsuranceOrderDAO;
import com.bobo.web.provider.dao.order.InsuranceSeckillDAO;
import com.bobo.web.provider.service.order.InsuranceOrderService;
import com.bobo.web.provider.util.JedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@Service
public class InsuranceOrderServiceImpl implements InsuranceOrderService {
    private static Logger LOGGER = LoggerFactory.getLogger(InsuranceOrderServiceImpl.class);
    @Resource
    private InsuranceOrderDAO insuranceOrderDAO;
    @Resource
    private InsuranceSeckillDAO insuranceSeckillDAO;
    @Resource
    private JedisUtils jedisUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Async("BasketballThreadPool")
    public ResponseResult<String> createInsuranceOrder(CreateSecKillParam createSecKillParam) throws Exception {
//        List<InsuranceSeckill> insuranceSeckills = insuranceSeckillDAO.selectByInsuranceId(createSecKillParam.getInsuranceId());
        String st = jedisUtils.hget(RedisConstant.INSURANCE_SEC_KILL + createSecKillParam.getInsuranceId(), "st");
        String et = jedisUtils.hget(RedisConstant.INSURANCE_SEC_KILL + createSecKillParam.getInsuranceId(), "et");
        String price = jedisUtils.hget(RedisConstant.INSURANCE_SEC_KILL + createSecKillParam.getInsuranceId(), "price");
        if(StringUtils.isEmpty(st)||StringUtils.isEmpty(et)){
            return ResponseResult.error("此产品暂时无秒杀场景");
        }
        if(System.currentTimeMillis()<Long.parseLong(st)){
            LOGGER.info("秒杀未开始");
            return ResponseResult.error("秒杀未开始");
        }
        if(System.currentTimeMillis()>Long.parseLong(et)){
            LOGGER.info("秒杀已结束,请关注活动");
            return ResponseResult.error("秒杀已结束,请关注活动");
        }
        if(StringUtils.isBlank(jedisUtils.lPop(RedisConstant.INSURANCE_SEC_KILL_COUNT+createSecKillParam.getInsuranceId()))){
            LOGGER.info("库存不足，请下次再来");
            return ResponseResult.error("库存不足，请下次再来");
        }
        LOGGER.info(String.format("库存还剩%s件",jedisUtils.getLenth(RedisConstant.INSURANCE_SEC_KILL_COUNT+createSecKillParam.getInsuranceId())));
        //创建订单
        String orderId = UUID.randomUUID().toString().replace("-","");
        InsuranceOrder insuranceOrder = new InsuranceOrder();
        insuranceOrder.setOrderId(orderId);
        insuranceOrder.setUserId(createSecKillParam.getUserId());
        insuranceOrder.setInsuranceId(createSecKillParam.getInsuranceId());
        insuranceOrder.setOrderNumber(1);
        insuranceOrder.setIsDel(0);
        insuranceOrder.setOrderPrice(Integer.valueOf(price));
        insuranceOrder.setPayStatus(0);
        insuranceOrder.setStatus(0);
        insuranceOrder.setCreateTime(new Date());
        insuranceOrder.setModifyTime(new Date());
        int insert = insuranceOrderDAO.insert(insuranceOrder);
        LOGGER.info(String.format("用户ID=%s，获取到保险一份，订单ID=%s,操作库存进行减一操作",createSecKillParam.getUserId(),orderId));
//        InsuranceSeckill insuranceSeckillUpdate = new InsuranceSeckill();
//        insuranceSeckillUpdate.setSeckillCount(seckillCount-1);
//        insuranceSeckillUpdate.setId(insuranceSeckill.getId());
//        insuranceSeckillUpdate.setVersion(insuranceSeckill.getVersion());
////        int updateResult = insuranceSeckillDAO.updateByPrimaryKeySelective(insuranceSeckillUpdate);
//        int updateResult = insuranceSeckillDAO.updateByPrimaryKeySelectiveNotVersion(insuranceSeckillUpdate);
//        LOGGER.info("更新库存返回的结果updateResult="+updateResult);
//        if(updateResult==0){
//            throw new Exception();
//        }
        return ResponseResult.success(orderId);
    }
}
